import Vue from 'vue';

import 'normalize.css/normalize.css'; // A modern alternative to CSS resets

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

import '@/styles/index.scss'; // global css

import App from './App';
import router from './router';
import store from './store';

import '@/icons'; // icon
import '@/permission'; // permission control

Vue.use(ElementUI);

Vue.config.productionTip = false;

/**
 * 判断是否有匹配项
 */
Vue.prototype.queryMatch = function(data) {
  let result = false;
  this.$store.getters.permissionId.forEach(item => {
    if (item === data) {
      result = true;
    }
  })
  return result;
}

import './api/system';
import { $config, $service, Service } from 'vue-element-simple';
import { getSaaSId } from './utils/auth'
// 重写请求方法, 加入mallId
Service.getService = async function (url, params, options = {}) {
  console.log('=========')
  console.log(getSaaSId())
  params.mallId = getSaaSId() || '';
  return await this.getSimpleService(`${$config.getApiPath()}${url}`, params, options);
};
$config.setConfig({
  // 接口请求地址
  API_PATH: process.env.BASE_API,
  // 上传地址
  UPLOAD_PATH: process.env.BASE_API + '/admin/image/upload',
  // 单个文件上传地址
  UPLOAD_SINGLE_FILE_PATH: process.env.BASE_API + '/admin/file/upload',
  // 请求公共参数
  REQUEST_COMMON_PARAMS: {
    // 请求混入公共参数
    token: () => $service.getToken(),
    mallId: getSaaSId()
  },
  // 获取历史图片列表方法
  'HISTORY_IMG_LIST_METHOD': async(page, size) => {
    const result = await $service.adminImageGetHistoryList({ page: page, pageSize: size });
    if (result.code === 0) {
      const { list, count } = result.data;
      const arr = [];
      for (let i = 0; i < list.length; i++) {
        const { id, name, url } = list[i];
        arr.push({ id, name, url });
      }
      return { list: arr, total: count };
    }
    return { list: [], total: 0 };
  },
  // 删除历史图片方法
  'HISTORY_IMG_DEL_METHOD': async(list) => {
    const arr = [];
    for (let i = 0; i < list.length; i++) {
      arr.push(list[i]['id']);
    }
    const result = await $service.adminImageDelHistory({ ids: arr.join(',') });
    return result.code === 0;
  }
})

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
